package com.have.blog.admin.controller;

import com.have.blog.admin.domain.dto.SystemDictDataDto;
import com.have.blog.admin.service.SystemDictDataService;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysDictData")
@Api(value = "字典数据相关接口", tags = {"字典数据相关接口"})
@Slf4j
public class SystemDictDataController {
    @Autowired
    private SystemDictDataService sysDictDataService;

    @ApiOperation(value = "根据字典类型获取字典数据", notes = "根据字典类型获取字典数据", response = String.class)
    @PostMapping("/getListByDictType")
    public Wrapper getListByDictType(@RequestParam("dictType") String dictType) {
        if (StringUtils.isEmpty(dictType)) {
            return Wrapper.illegal();
        }
        Map<String, Object> map = sysDictDataService.getListByDictType(dictType);
        if (map == null) {
            return Wrapper.failed();
        }
        return Wrapper.success(map);
    }

    @ApiOperation(value = "根据字典类型数组获取字典数据", notes = "根据字典类型数组获取字典数据", response = String.class)
    @PostMapping("/getListByDictTypeList")
    public Wrapper getListByDictTypeList(@RequestBody List<String> dictTypeList) {
        if (CollectionUtils.isEmpty(dictTypeList)) {
            return Wrapper.illegal();
        }
        Map<String, Map<String, Object>> map = sysDictDataService.getDataListByDictTypeList(dictTypeList);
        if (map == null) {
            return Wrapper.failed();
        }
        return Wrapper.success(map);
    }

    @ApiOperation(value = "根据字典类型数组获取字典数据", notes = "根据字典类型数组获取字典数据")
    @PostMapping("/getList")
    public Wrapper<CommonPage<SystemDictDataDto>> getList(@Validated({GetList.class})@RequestBody SystemDictDataDto systemDictDataDto) {
        return sysDictDataService.getList(systemDictDataDto);
    }

    @ApiOperation(value = "添加字典类型数据", notes = "添加字典类型数据")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class})@RequestBody SystemDictDataDto systemDictDataDto) {
        return sysDictDataService.add(systemDictDataDto);
    }

    @ApiOperation(value = "添加字典类型数据", notes = "添加字典类型数据")
    @PostMapping("/deleteBatch")
    public Wrapper delete(List<Integer> uids) {
        return sysDictDataService.delete(uids);
    }

    @ApiOperation(value = "添加字典类型数据", notes = "添加字典类型数据")
    @PostMapping("/edit")
    public Wrapper edit(@RequestBody SystemDictDataDto systemDictDataDto) {
        System.out.println(systemDictDataDto.getUid());
        return sysDictDataService.edit(systemDictDataDto);
    }
}
